import configparser
import logging as log
class Conf:
    conf = configparser.ConfigParser()  # 类变量

    def read_entry(self):  # 读入口配置文件
        Conf.conf.read('../conf/entry.ini', encoding='utf-8')
        self.__which_server = Conf.conf.get('entry', 'which_server')
        self.__which_db = Conf.conf.get('entry', 'which_db')
        # print(self.__which_server, self.__which_db)

    def read_server_conf(self):  # 读接口服务器信息  将接口服务器信息组装成http://IP:port的形式，向类外公开，后面用例中可以使用
        Conf.conf.read('../conf/server.conf', encoding='utf-8')
        ip = Conf.conf.get(self.__which_server, 'ip')
        port = Conf.conf.get(self.__which_server, 'port')
        self.host = f'http://{ip}:{port}'
        # print(self.host)

    def read_db_conf(self):  # 读数据库
        Conf.conf.read('../conf/db.conf', encoding='utf-8')
        host = Conf.conf.get(self.__which_db, 'host')
        db = Conf.conf.get(self.__which_db, 'db')
        user = Conf.conf.get(self.__which_db, 'user')
        passwd = Conf.conf.get(self.__which_db, 'passwd')
        self.dbinfo = {'host': host, 'db': db, 'user': user, 'passwd': passwd}
        # print(self.dbinfo)

    def __init__(self):  # 构造方法
        try:
            self.read_entry()
            self.read_server_conf()
            self.read_db_conf()
            log.info(f'接口服务器信息：{self.host}==数据库服务器信息：{self.dbinfo}')
        except BaseException as e:
            log.error(f'获得服务器信息出错==错误类型：{type(e).__name__}==错误内容：{e}')
            exit()  #退出测试

if __name__=='__main__':
    a=Conf()
    print(a.host,a.dbinfo)
